﻿@using Models
@model List<News>
@{
    var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
    FormsAuthenticationTicket auth = null;
    if (cookie != null)
    {
        auth = FormsAuthentication.Decrypt(cookie.Value);

        ViewBag.auth = auth;
    }

    ViewBag.Title = "All news";
}
 

  @Html.Partial("_openDialog")

<div style="float: left; height: 70px"><a href="/admin/index" ><img src="/Content/images/back.png" /></a></div>
<div style="margin-left: 80px;"><a href="/admin/AddNews" ><img src="/content/images/news_add.png" alt="Add News" /><br />Add news</a></div>
<h2>All news</h2>
@if (ViewBag.Categories != null)
{
@Html.Partial("_filter_news")
    if (Model != null)
    {
    <table id="table_admin">
        <tr>
            <td>
                ID
            </td>
            <td style="wIdth: 90px;">
                @Html.ActionLink("Title", "AllNews", "admin", new { page = ViewBag.pager.CurrentPage, orderBy = "title" }, null)
            </td>
        
     
            <td style="wIdth: 80px;">
                @Html.ActionLink("Author", "AllNews", "admin", new { page = ViewBag.pager.CurrentPage, orderBy = "author" }, null)
            </td>
            <td>
                @Html.ActionLink("Date of last update", "AllNews", "admin", new { page = ViewBag.pager.CurrentPage, orderBy = "updateDate" }, null)
            </td>
            <td>
                @Html.ActionLink("Date of publication", "AllNews", "admin", new { page = ViewBag.pager.CurrentPage, orderBy = "publishDate" }, null)
            </td>
            <td>
                @Html.ActionLink("Rating", "AllNews", "admin", new { page = ViewBag.pager.CurrentPage, orderBy = "rating" }, null)
            </td>
            <td>
                Action button
            </td>
        </tr>
  
        @for (int i = ViewBag.pager.StartIndex - 1; i <= ViewBag.pager.EndIndex - 1; i++)
        {

            <tr>    
              
                <td>
                    @Model[i].Id 
                </td>
                <th>
                    @{
                        @Html.ActionLink(Model[i].Title, "Index", "Home", new { id = Model[i].Id }, null)
                    }
                </th>
               
              
                <td>@{
            string author = Model[i].Author;
                        @Html.ActionLink(author, "Index", "Users", new { id = author }, null)
                    }
                </td>
                <th>
                    @{
            if (Model[i].UpdateDate != null)
            {
                var d = Convert.ToDateTime(Model[i].UpdateDate);


                            @d.Date.GetDateTimeFormats()[15]
            }
                    }
                </th>
                <td>
                    @{
            if (Model[i].PublishDate != null)
            {
                var p = Convert.ToDateTime(Model[i].PublishDate);

                            @p.Date.GetDateTimeFormats()[15]
            }
                    }
                </td>
                <td>
                    @Model[i].Rating
                </td>
                <th>
                        
                    <a href="/admin/updateNews/@Model[i].Id"><img src="/content/images/Refresh.png" alt="Update" /></a>

                   
                    @if (auth.UserData == "1")
                    {
                        int id = @Model[i].Id;
                        <a href="/admin/deleteNews/@Model[i].Id" onmousedown=" return openDialog(@id,'/Admin/DeleteNews');"><img src="/content/images/Delete.png" alt="Delete" /></a>
                    }
                    <span></span>
                    @if (Model[i].PublishDate == null)
                    {

                        <a href="/admin/publishNews/@Model[i].Id"><img src="/content/images/upload.png" alt="Publish" /></a>
                    }
                </th>
               
            </tr>

        }
        
   
    </table>
    }
    else
    {
        <span>No news!</span>
    }
}
else
{
    <span>@Html.ActionLink("NO category! PLZ ADD", "AddCategory", "Admin")</span>
}